package com.pks.datacenter.service.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.config.RedisKeyConfig;
import com.pks.datacenter.entity.basic.DbInfo;
import com.pks.datacenter.entity.basic.Tag;
import com.pks.datacenter.handler.RedisHandler;
import com.pks.datacenter.mapper.basic.TagMapper;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("tagService")
public class TagServiceImpl extends BasicServiceImpl<Tag, Long, TagMapper> implements TagService {

	@Autowired
	private TagMapper tagMapper;
	@Autowired
	private RedisHandler redisHandler;
	
	@Override
	protected TagMapper getMapper() {
		return tagMapper;
	}

	@Override
	public List<Tag> findByType(Integer type) {
		List<Tag> list = new ArrayList<Tag>();
		if (type != null) {
			Object obj = redisHandler.get(RedisKeyConfig.KEY_LIST_TAGS_PREFIX + type);
			if (obj != null) {
				list = (List<Tag>) obj;
			} else {
				Map<String, Object> qbuilder = new HashMap<String, Object>();
				qbuilder.put("type", type);
				qbuilder.put("state", 1);
				list = tagMapper.find(qbuilder, null, null, "orders asc");
				redisHandler.set(RedisKeyConfig.KEY_LIST_TAGS_PREFIX + type, list, RedisKeyConfig.CACHE_TIME);
			}
		}
		return list;
	}
	
	@Override
	public Map<Long,Tag> findTagMap(Integer type) {
		Map<Long,Tag> map = new HashMap<Long, Tag>();
		if (type != null ) {
			List<Tag> list = findByType(type);
			for (Tag tag : list) {
				map.put(tag.getId(), tag);
			}
		}
		return map;
	}
	
	
}
